import 'muse-ui/dist/muse-ui.css'
import 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import store from './vuex/store'
import MuseUI from 'muse-ui'
import * as filters from '@/common/js/filters'
import {GetRequest} from './common/js/util'
import VueWechatTitle from 'vue-wechat-title'
// Add lazyload directive
import VueLazyload from 'vue-lazyload'
import errorImg from './assets/images/head_images2.png'
import {APP_PLATFORM} from '@/api/api'
import router from './router'

Vue.config.devtools = true

Vue.use(VueWechatTitle)
// register filters
for (var f in filters) {
  Vue.filter(f, filters[f])
}

MuseUI.config({
  disableTouchRipple: false,
  disableFocusRipple: true
})
Vue.use(MuseUI)
Vue.use(VueLazyload, {
  error: errorImg,
  loading: errorImg
})
/**
 * Mock server for devolopment
 * import Mock from './mock'
 * Mock.start()
*/
/**
 * 环境适配
 * 支持微信公众号和支付宝生活号
 */
const APP_PLATFORM_NAME = ['', 'openId', 'aliUid'][APP_PLATFORM]
Vue.prototype.$APP_PLATFORM = APP_PLATFORM
Vue.prototype.$APP_PLATFORM_NAME = APP_PLATFORM_NAME
Vue.prototype.$log = (msg) => console.log(msg)
/**
 * 导航钩子
 */
router.beforeEach((to, from, next) => {
  const querystring = GetRequest(to.fullPath)
  if (querystring[APP_PLATFORM_NAME] !== undefined) {
    sessionStorage[APP_PLATFORM_NAME] = querystring[APP_PLATFORM_NAME]
  }
  let query = {
    redirect: to.fullPath,
    [APP_PLATFORM_NAME]: sessionStorage[APP_PLATFORM_NAME]
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (sessionStorage.accessToken) {
      next()
    } else {
      next({
        path: '/login',
        query: query
      })
    }
  } else {
    next()
  }
})

Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
